package com.suray.wcs.service.pojo.rgv.rgv.protocol.util;

import com.suray.basic.wcs.utils.ParamConfig;

public enum RgvMessageType {
    RGV_TASK_MESSAGE(1, "整体任务"),
    RGV_COMMAND_MESSAGE(2, "指令"),
    RGV_DEBUG_CMD_MESSAGE(3, "调试指令"),
    RGV_MAP_MESSAGE(4, "地图"),
    RGV_SCADA_MESSAGE(5, "SCADA"),
    RGV_CHANGE_LORA_MESSAGE(6, "修改Lora配置"),
    RGV_CLEAR_ORDER_MESSAGE(7, "修改Lora配置"),
    RGV_LIFE_MESSAGE(10, "心跳"),
    ;

    private String des;

    private int value;

    RgvMessageType (int value, String des) {
        this.des = des;
        this.value = value;
    }

    public String getDes() {
        return des;
    }

    public int getValue() {
        if (this == RgvMessageType.RGV_TASK_MESSAGE) {
            RgvTaskMessageType taskMessageType = analyzeRgvTaskMessageType();
            switch (analyzeRgvTaskMessageType()) {
                case NORMAL_TASK_MESSAGE:
                case EXTERN_TASK_NODE_MESSAGE:
                case EX_TASK_NODE_WITH_SPEED_MSG:
                    return taskMessageType.getValue();
                default:
                    throw new RuntimeException("任务报文类型配置错误，请查看application中rgv-rgvTaskMessageType配置是否正确");
            }
        }

        return value;
    }

    public static RgvMessageType getType(int value) {
        for (RgvMessageType type : RgvMessageType.values()) {
            if (type.getValue() == value) {
                return type;
            }
        }

        return null;
    }

    public static RgvTaskMessageType analyzeRgvTaskMessageType() {
        RgvTaskMessageType rgvTaskMessageType = RgvTaskMessageType.parseValue(ParamConfig.rgvTaskMessageType);
        if (rgvTaskMessageType == RgvTaskMessageType.INVALID_MESSAGE_TYPE) {
            return RgvTaskMessageType.NORMAL_TASK_MESSAGE;
        }

        return rgvTaskMessageType;
    }
}
